package com.example.data_receiver.repository;


import com.example.data_receiver.entity.YfzkRecord;
import com.example.data_receiver.entity.YfzlRecord;
import com.example.data_receiver.entity.YszkRecord;
import com.example.data_receiver.entity.YszlRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

@Repository
public class DorisRepository {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    private static final String YSZK_INSERT_SQL = "INSERT INTO dwd_yszk_detail " +
            "(bukrs, kunnr, zzhth, zzhtmc, projk, post1, hkont, hkont_txt, wrbtr, dmbtr, " +
            "waers, kursf, budat, zfbdt, purpose, gjahr, belnr, buzei, shkzg, zterm, " +
            "zsystem, orderid, hkont_yj, timestamp) " +
            "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

    private static final String YFZK_INSERT_SQL = "INSERT INTO dwd_yfzk_detail_last (" +
            "bukrs, belnr, gjahr, contract_url, buzei, lifnr, zzhth, projk, zzhtmc, post1, " +
            "hkont, hkont_txt, wrbtr, dmbtr, waers, kursf, budat, zfbdt, purpose, shkzg, " +
            "zterm, zsystem, orderid, hkont_yj, `timestamp`" +
            ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

    private static final String YFZL_INSERT_SQL = "INSERT INTO dwd_yfzl (" +
            "bukrs, jtkm, ktokd, ktokd_txt, lifnr, lifnr_txt, land1, hkont, hkont_txt, " +
            "umskz, bukrs_txt, prctr, prctr_txt, pernr, pernr_txt, aufnr, aufnr_txt, " +
            "projk, projk_txt, belnr, buzei, gjahr, blart, tcode, xref1, zuonr, sgtxt, " +
            "bldat, budat, zfbdt, augbl, augdt, khye, wdqje, zlje, wrbtr_01, wrbtr_02, " +
            "wrbtr_03, wrbtr_04, wrbtr_05, wrbtr_06, zzhth, zzhthmc, yjkm, `timestamp`" +
            ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";


    private static final String YSZL_INSERT_SQL = "INSERT INTO dwd_yszl (" +
            "bukrs, jtkm, ktokd, ktokd_txt, kunnr, kunnr_txt, land1, hkont, hkont_txt, " +
            "umskz, bukrs_txt, prctr, prctr_txt, pernr, pernr_txt, aufnr, aufnr_txt, " +
            "projk, projk_txt, belnr, buzei, gjahr, blart, tcode, xref1, zuonr, sgtxt, " +
            "bldat, budat, zfbdt, augbl, augdt, khye, wdqje, zlje, wrbtr_01, wrbtr_02, " +
            "wrbtr_03, wrbtr_04, wrbtr_05, wrbtr_06, zzhth, zzhthmc, yjkm, `timestamp`" +
            ") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

    public void batchInsertYszk(List<YszkRecord> records) {
        jdbcTemplate.batchUpdate(YSZK_INSERT_SQL, new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps, int i) throws SQLException {
                YszkRecord record = records.get(i);
                ps.setString(1, record.getBukrs());
                ps.setString(2, record.getKunnr());
                ps.setString(3, record.getZzhth());
                ps.setString(4, record.getZzhtmc());
                ps.setString(5, record.getProjk());
                ps.setString(6, record.getPost1());
                ps.setString(7, record.getHkont());
                ps.setString(8, record.getHkont_txt());
                ps.setBigDecimal(9, record.getWrbtr());
                ps.setBigDecimal(10, record.getDmbtr());
                ps.setString(11, record.getWaers());
                ps.setBigDecimal(12, record.getKursf());
                ps.setString(13, record.getBudat());
                ps.setString(14, record.getZfbdt());
                ps.setString(15, record.getPurpose());
                ps.setInt(16, record.getGjahr());
                ps.setString(17, record.getBelnr());
                ps.setInt(18, record.getBuzei());
                ps.setString(19, record.getShkzg());
                ps.setString(20, record.getZterm());
                ps.setString(21, record.getZsystem());
                ps.setString(22, record.getOrderid());
                ps.setString(23, record.getHkont_yj());
                ps.setString(24, record.getTimestamp());
            }

            @Override
            public int getBatchSize() {
                return records.size();
            }
        });
    }


    public void batchInsertYfzk(List<YfzkRecord> records) {
        jdbcTemplate.batchUpdate(YFZK_INSERT_SQL, new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps, int i) throws SQLException {
                YfzkRecord record = records.get(i);
                ps.setString(1, record.getBukrs());
                ps.setString(2, record.getBelnr());
                ps.setInt(3, record.getGjahr());
                ps.setString(4, record.getContract_url());
                ps.setInt(5, record.getBuzei());
                ps.setString(6, record.getLifnr());
                ps.setString(7, record.getZzhth());
                ps.setString(8, record.getProjk());
                ps.setString(9, record.getZzhtmc());
                ps.setString(10, record.getPost1());
                ps.setString(11, record.getHkont());
                ps.setString(12, record.getHkont_txt());
                ps.setBigDecimal(13, record.getWrbtr());
                ps.setBigDecimal(14, record.getDmbtr());
                ps.setString(15, record.getWaers());
                ps.setBigDecimal(16, record.getKursf());
                ps.setString(17, record.getBudat());
                ps.setString(18, record.getZfbdt());
                ps.setString(19, record.getPurpose());
                ps.setString(20, record.getShkzg());
                ps.setString(21, record.getZterm());
                ps.setString(22, record.getZsystem());
                ps.setString(23, record.getOrderid());
                ps.setString(24, record.getHkont_yj());
                ps.setString(25,record.getTimestamp());
            }

            @Override
            public int getBatchSize() {
                return records.size();
            }
        });
    }

    public void batchInsertYfzl(List<YfzlRecord> records) {
        jdbcTemplate.batchUpdate(YFZL_INSERT_SQL, new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps, int i) throws SQLException {
                YfzlRecord record = records.get(i);
                ps.setString(1, record.getBukrs());
                ps.setString(2, record.getJtkm());
                ps.setString(3, record.getKtokd());
                ps.setString(4, record.getKtokd_txt());
                ps.setString(5, record.getLifnr());
                ps.setString(6, record.getLifnr_txt());
                ps.setString(7, record.getLand1());
                ps.setString(8, record.getHkont());
                ps.setString(9, record.getHkont_txt());
                ps.setString(10, record.getUmskz());
                ps.setString(11, record.getBukrs_txt());
                ps.setString(12, record.getPrctr());
                ps.setString(13, record.getPrctr_txt());
                ps.setString(14, record.getPernr());
                ps.setString(15, record.getPernr_txt());
                ps.setString(16, record.getAufnr());
                ps.setString(17, record.getAufnr_txt());
                ps.setString(18, record.getProjk());
                ps.setString(19, record.getProjk_txt());
                ps.setString(20, record.getBelnr());
                ps.setInt(21, record.getBuzei());
                ps.setInt(22, record.getGjahr());
                ps.setString(23, record.getBlart());
                ps.setString(24, record.getTcode());
                ps.setString(25, record.getXref1());
                ps.setString(26, record.getZuonr());
                ps.setString(27, record.getSgtxt());
                ps.setString(28, record.getBldat());
                ps.setString(29, record.getBudat());
                ps.setString(30, record.getZfbdt());
                ps.setString(31, record.getAugbl());
                ps.setString(32, record.getAugdt());
                ps.setBigDecimal(33, record.getKhye());
                ps.setBigDecimal(34, record.getWdqje());
                ps.setBigDecimal(35, record.getZlje());
                ps.setBigDecimal(36, record.getWrbtr_01());
                ps.setBigDecimal(37, record.getWrbtr_02());
                ps.setBigDecimal(38, record.getWrbtr_03());
                ps.setBigDecimal(39, record.getWrbtr_04());
                ps.setBigDecimal(40, record.getWrbtr_05());
                ps.setBigDecimal(41, record.getWrbtr_06());
                ps.setString(42, record.getZzhth());
                ps.setString(43, record.getZzhthmc());
                ps.setString(44, record.getYjkm());
                ps.setString(45,record.getTimestamp());
            }

            @Override
            public int getBatchSize() {
                return records.size();
            }
        });
    }

    public void batchInsertYszl(List<YszlRecord> records) {
        jdbcTemplate.batchUpdate(YSZL_INSERT_SQL, new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps, int i) throws SQLException {
                YszlRecord record = records.get(i);
                ps.setString(1, record.getBukrs());
                ps.setString(2, record.getJtkm());
                ps.setString(3, record.getKtokd());
                ps.setString(4, record.getKtokd_txt());
                ps.setString(5, record.getKunnr());
                ps.setString(6, record.getKunnr_txt());
                ps.setString(7, record.getLand1());
                ps.setString(8, record.getHkont());
                ps.setString(9, record.getHkont_txt());
                ps.setString(10, record.getUmskz());
                ps.setString(11, record.getBukrs_txt());
                ps.setString(12, record.getPrctr());
                ps.setString(13, record.getPrctr_txt());
                ps.setString(14, record.getPernr());
                ps.setString(15, record.getPernr_txt());
                ps.setString(16, record.getAufnr());
                ps.setString(17, record.getAufnr_txt());
                ps.setString(18, record.getProjk());
                ps.setString(19, record.getProjk_txt());
                ps.setString(20, record.getBelnr());
                ps.setInt(21, record.getBuzei());
                ps.setInt(22, record.getGjahr());
                ps.setString(23, record.getBlart());
                ps.setString(24, record.getTcode());
                ps.setString(25, record.getXref1());
                ps.setString(26, record.getZuonr());
                ps.setString(27, record.getSgtxt());
                ps.setString(28, record.getBldat());
                ps.setString(29, record.getBudat());
                ps.setString(30, record.getZfbdt());
                ps.setString(31, record.getAugbl());
                ps.setString(32, record.getAugdt());
                ps.setBigDecimal(33, record.getKhye());
                ps.setBigDecimal(34, record.getWdqje());
                ps.setBigDecimal(35, record.getZlje());
                ps.setBigDecimal(36, record.getWrbtr_01());
                ps.setBigDecimal(37, record.getWrbtr_02());
                ps.setBigDecimal(38, record.getWrbtr_03());
                ps.setBigDecimal(39, record.getWrbtr_04());
                ps.setBigDecimal(40, record.getWrbtr_05());
                ps.setBigDecimal(41, record.getWrbtr_06());
                ps.setString(42, record.getZzhth());
                ps.setString(43, record.getZzhthmc());
                ps.setString(44, record.getYjkm());
                ps.setString(45, record.getTimestamp());
            }

            @Override
            public int getBatchSize() {
                return records.size();
            }
        });
    }
}    